package mz.matafe.bolao

import org.codehaus.groovy.grails.plugins.springsecurity.Secured

/**
 * Group Controller.
 *
 * <p>Handle the groups
 *
 * @since 0.0.1
 * @author <a href="mailto:matafe@gmail.com">Maurício T. Ferraz</a>
 */
class GroupController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    @Secured(['ROLE_ADMIN','ROLE_SUPERVISOR'])
    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [groups: Group.list(params), groupTotal: Group.count()]
    }

    @Secured(['ROLE_ADMIN'])
    def create = {
        def group = new Group()
        group.properties = params
        [group: group]
    }

    @Secured(['ROLE_ADMIN'])
    def save = {
        def group = new Group(params)
        if (group.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'group.label', default: 'Group'), group.name])}"
            redirect(action: "show", id: group.id)
        }
        else {
            render(view: "create", model: [group: group])
        }
    }
    
    @Secured(['ROLE_ADMIN','ROLE_SUPERVISOR'])
    def show = {
        def group = Group.get(params.id)
        if (!group) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'group.label', default: 'Group'), params.id])}"
            redirect(action: "list")
        }
        else {
            [group: group]
        }
    }

    @Secured(['ROLE_ADMIN'])
    def edit = {
        def group = Group.get(params.id)
        if (!group) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'group.label', default: 'Group'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [group: group]
        }
    }

    @Secured(['ROLE_ADMIN'])
    def update = {
        def group = Group.get(params.id)
        if (group) {
            if (params.version) {
                def version = params.version.toLong()
                if (group.version > version) {
                    group.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'group.label', default: 'Group')] as Object[], "Another user has updated this Group while you were editing")
                    render(view: "edit", model: [group: group])
                    return
                }
            }
            group.properties = params
            if (!group.hasErrors() && group.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'group.label', default: 'Group'), group.name])}"
                redirect(action: "show", id: group.id)
            }
            else {
                render(view: "edit", model: [group: group])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'group.label', default: 'Group'), params.id])}"
            redirect(action: "list")
        }
    }

    @Secured(['ROLE_ADMIN'])
    def delete = {
        def group = Group.get(params.id)
        if (group) {
            try {
                group.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'group.label', default: 'Group'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'group.label', default: 'Group'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'group.label', default: 'Group'), params.id])}"
            redirect(action: "list")
        }
    }
}
